package io.zt.dangyuan;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import io.zt.dangyuan.object.DetailObject;

import org.json.JSONObject;
import org.xutils.ex.HttpException;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

public class BBSDetailActivity extends BaseActivity{

    ImageView detailImg;
    WebView webView;
    public static final int TYPE_COMMENT = 1;//评论
    public static final int TYPE_BAOMING = 2;//报名
    public static final int TYPE_ZHONGCHOU = 3;//众筹
    public static final int TYPE_YUDING = 4;//预定
    TextView feedbackSubmit;
    EditText feedbackContent;
    public int type;

    private int contentid;
    private DetailObject resultData;
    private String count = "0";
    private String cid;
    private String cname;
    //
    private String url = "content.json?";
    private String url1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addCenterView(R.layout.activity_detail);
//        detailImg = (ImageView) findViewById(R.id.detail_img);
        webView = (WebView) findViewById(R.id.detail_webview);
        feedbackContent = (EditText) findViewById(R.id.feedback_content);
        feedbackSubmit = (TextView) findViewById(R.id.feedback_submit);
        feedbackSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webSendRequst();
            }
        });
        initData();
        webRequst();
    }

    private void initData() {
        setTitle("信息详情");
        toolbar.setNavigationIcon(R.drawable.icon_pre);
        Intent intent = getIntent();
        contentid = intent.getIntExtra("contentid", 0);
        url1 =url+"contentid"+"="+contentid+"&cid=hdfb";
        cid = intent.getStringExtra("cid");
//        count = intent.getStringExtra("commotcount");
        // type=2;
        Log.e("bbbb", url1);
        cname = intent.getStringExtra("cname");
        int id = intent.getIntExtra("id",-1);
        WebUtils.sendGet(BBSDetailActivity.this, url1,new HashMap<String,Object>(), new WebUtils.OnWebListenr() {
            @Override
            public void onSuccess(boolean isSuccess, String msg, String data) {
                String result = data;
                try {
                    JSONObject object = new JSONObject(result);
                    JSONObject json = object.getJSONObject("data");

                    String modelid = json.getString("modelid");
                    Log.e("AAAAA",modelid);
                    if (modelid.equals("9")) {
                        type=2;
                        Log.e("CCCC","22222");
                        switch (type) {
                            //1
                            case TYPE_COMMENT:
                                feedbackSubmit.setText("评论");
                                feedbackContent.setHint("输入评论内容");
                                break;
                            //2
                            case TYPE_BAOMING:
                                feedbackSubmit.setText("报名");
                                feedbackContent.setHint("输入报名内容");
                                break;
                            //3
                            case TYPE_ZHONGCHOU:
                                feedbackSubmit.setText("参与众筹");
                                feedbackContent.setHint("输入众筹留言内容");
                                break;
                            //4
                            case TYPE_YUDING:
                                feedbackSubmit.setText("预定");
                                feedbackContent.setHint("请输入产品数量");
                                break;
                        }
                    }else {
                        Intent intent = getIntent();
                        type = intent.getIntExtra("type", 0);
                        switch (type) {
                            //1
                            case TYPE_COMMENT:
                                feedbackSubmit.setText("评论");
                                feedbackContent.setHint("输入评论内容");
                                break;
                            //2
                            case TYPE_BAOMING:
                                feedbackSubmit.setText("报名");
                                feedbackContent.setHint("输入报名内容");
                                break;
                            //3
                            case TYPE_ZHONGCHOU:
                                feedbackSubmit.setText("参与众筹");
                                feedbackContent.setHint("输入众筹留言内容");
                                break;
                            //4
                            case TYPE_YUDING:
                                feedbackSubmit.setText("预定");
                                feedbackContent.setHint("请输入产品数量");
                                break;
                        }
                    }

                    // pd.dismiss();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(HttpException error, String msg) {
                Toast.makeText(BBSDetailActivity.this, "数据请求失败" + msg, Toast.LENGTH_LONG).show();
            }
        });
        if( id != -1){
            PushTQReceiver.titles.remove(id);
        }
//        switch (type) {
//            //1
//            case TYPE_COMMENT:
//                feedbackSubmit.setText("评论");
//                feedbackContent.setHint("输入评论内容");
//                break;
//            //2
//            case TYPE_BAOMING:
//                feedbackSubmit.setText("报名");
//                feedbackContent.setHint("输入报名内容");
//                break;
//            //3
//            case TYPE_ZHONGCHOU:
//                feedbackSubmit.setText("参与众筹");
//                feedbackContent.setHint("输入众筹留言内容");
//                break;
//            //4
//            case TYPE_YUDING:
//                feedbackSubmit.setText("预定");
//                feedbackContent.setHint("请输入产品数量");
//                break;
//        }
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {


            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_detail_comment, menu);

        String str = "";
        switch (type) {
            case TYPE_COMMENT:
                str = "条评论";
                break;
            case TYPE_BAOMING:
                str = "人报名";
                break;
            case TYPE_ZHONGCHOU:
                str = "人参与众筹";
                break;
            case TYPE_YUDING:
                str = "预定人数";
                break;
        }
        menu.getItem(0).setTitle(count + str);
        return true;
    }

    public Boolean check() {
        if (TextUtils.isEmpty(feedbackContent.getText())) {

            String str = null;
            switch (type) {
                case BBSDetailActivity.TYPE_COMMENT:
                    str = "评论";

                    break;
                case BBSDetailActivity.TYPE_BAOMING:
                    str = "报名";

                    break;
                case BBSDetailActivity.TYPE_ZHONGCHOU:
                    str = "众筹";
                    break;
                case BBSDetailActivity.TYPE_YUDING:
                    str = "预定";
                    break;
            }

            Toast.makeText(this, str + "内容不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                come_back();
                break;
            case R.id.action_comments:
                Intent intent = new Intent(this, BBSCommentActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("contentid", contentid);
                startActivity(intent);

                break;
        }
        return true;
    }

    public void webRequst() {
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("contentid", String.valueOf(contentid));
        map.put("cid", cid);

        WebUtils.sendPost(BBSDetailActivity.this, "content", map, new WebUtils.OnWebListenr() {
            @Override
            public void onSuccess(boolean isSuccess, String msg, String data) {

                resultData = JSON.parseObject(data, DetailObject.class);
                count = String.valueOf(resultData.getComments());
                //Log.e("aaaaa",resultData.getComments());
                invalidateOptionsMenu();
                webView.loadUrl(resultData.getUrl());

            }

            @Override
            public void onFailed(HttpException error, String msg) {

            }
        });
    }

    public void webSendRequst() {

        boolean isgo = check();
        if (!isgo) {
            return;
        }
        final GlobalDataApplication app = (GlobalDataApplication) getApplication();

        if (app.getUserModel() == null) {
            startActivity(new Intent(BBSDetailActivity.this, LoginActivity.class));
            return;
        }
        String conmentStr = "";
        try {
            conmentStr = URLEncoder.encode(feedbackContent.getText().toString(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("contentid", String.valueOf(contentid));
        map.put("text", conmentStr);
        map.put("userid", String.valueOf(app.getUserModel().getUserid()));

        WebUtils.sendPost(BBSDetailActivity.this, "comment", map, new WebUtils.OnWebListenr() {
            @Override
            public void onSuccess(boolean isSuccess, String msg, String data) {

                if (isSuccess) {

                    String dataStr = "评论成功";
                    switch (type) {
                        case BBSDetailActivity.TYPE_COMMENT:
                            dataStr = "评论成功";

                            break;
                        case BBSDetailActivity.TYPE_BAOMING:
                            dataStr = "报名成功";

                            break;
                        case BBSDetailActivity.TYPE_ZHONGCHOU:
                            dataStr = "众筹成功";
                            break;
                        case BBSDetailActivity.TYPE_YUDING:
                            dataStr = "请保持手机畅通,将有工作人员与您联系";
                            break;
                    }


                    Toast.makeText(BBSDetailActivity.this, dataStr, Toast.LENGTH_SHORT).show();
                    feedbackContent.setText("");
                    // 评论完成
                    int countt = Integer.parseInt(count);
                    countt++;
                    count = String.valueOf(countt);
//                    count+=1;
                    webRequst();
                } else {
                    Toast.makeText(BBSDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailed(HttpException error, String msg) {
                Toast.makeText(BBSDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            come_back();
        }
        return true;
    }


    public void come_back(){
        if (cname != null && !cname.equals("")) {
            switch (cname) {
                case "djdt":
                    startActivity(new Intent(this, ActivityItem1.class).putExtra("pos", 6));
                    break;
                case "ttyw":
                    startActivity(new Intent(this, ActivityItem2.class).putExtra("titles", MainActivity.title_TQ)
                            .putExtra("pos", 5).putExtra("index", 0).putExtra("cid",cname));
                    break;
                case "txyw":
                    startActivity(new Intent(this, ActivityItem2.class).putExtra("titles", MainActivity.title_TQ)
                            .putExtra("pos", 5).putExtra("index", 1).putExtra("cid",cname));
                    break;
                case "qnzs":
                    startActivity(new Intent(this, ActivityItem2.class).putExtra("titles", MainActivity.title_TQ)
                            .putExtra("pos", 5).putExtra("index", 2).putExtra("cid",cname));
                    break;
                case "pxhd":
                    startActivity(new Intent(this, ActivityItem2.class).putExtra("pos", 1)
                            .putExtra("titles", MainActivity.title_HD).putExtra("index", 0).putExtra("cid",cname));
                    break;
                case "hdfb":
                    startActivity(new Intent(this, ActivityItem2.class).putExtra("pos", 1)
                            .putExtra("titles", MainActivity.title_HD).putExtra("index", 1).putExtra("cid",cname));
                    break;
                case "hdhg":
                    startActivity(new Intent(this, ActivityItem2.class).putExtra("pos", 1)
                            .putExtra("titles", MainActivity.title_HD).putExtra("index", 2).putExtra("cid",cname));
                    break;
                case "zcgy":
                    startActivity(new Intent(this, ActivityItem1.class).putExtra("pos", 2));
                    break;
                case "xljkcp":
                    startActivity(new Intent(this, ActivityItem2.class).putExtra("pos", 0)
                            .putExtra("titles", MainActivity.title_QN).putExtra("index", 0).putExtra("cid",cname));
                    break;
                case "xljkfd":
                    startActivity(new Intent(this, ActivityItem2.class).putExtra("pos", 0)
                            .putExtra("titles", MainActivity.title_QN).putExtra("index", 1).putExtra("cid",cname));
                    break;
                case "dklc":
                    startActivity(new Intent(this, ActivityLife.class).putExtra("index", 0).putExtra("cid", cname));
                    break;
                case "jcksp":
                    startActivity(new Intent(this, ActivityLife.class).putExtra("index", 1).putExtra("cid", cname));
                    break;
                case "shkx":
                    startActivity(new Intent(this, ActivityLife.class).putExtra("index", 2).putExtra("cid", cname));
                    break;
                case "lysh":
                    startActivity(new Intent(this, ActivityLife.class).putExtra("index", 3).putExtra("cid", cname));
                    break;
                case "shyl":
                    startActivity(new Intent(this, ActivityLife.class).putExtra("index", 4).putExtra("cid",cname));
                    break;
                case "gzbs":
                    startActivity(new Intent(this, ActivityItem1.class).putExtra("pos", 7));
                    break;
            }
        }
        finish();
    }
}
